package com.wanggsx.library.base.views;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TextView;

/**
 * Created by wanggsx on 15/10/14.
 */
public class SuperTextView extends View {

    private Paint mPaint = null;
    private int mCenterX = -1, mCenterY;
    private TextView mTview;

    public SuperTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public void setText(TextView textView, int Color) {
        if (mPaint == null) {
            mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        }
        mPaint.setColor(getResources().getColor(Color));
        mTview = textView;
        invalidate();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        if (mPaint != null && mTview != null) {
            if (mCenterX == -1) {
                mCenterX = getWidth() / 2;
                mCenterY = getHeight() / 2;
            }
            mTview.buildDrawingCache();
            Bitmap bp = mTview.getDrawingCache();
            canvas.drawBitmap(bp, mCenterX - bp.getWidth() / 2, mCenterY - bp.getHeight() / 2, mPaint);
        }
    }
}
